/*******************************************************************************
 * Copyright (c) 2010 Gary Yund {OTHER COPYRIGHT OWNERS}.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Gary Yund - initial API and implementation and/or initial documentation
 *******************************************************************************/

#import "BB_CSVParserTestCase.h"

#import "BB_CSVParser.h"


@implementation BB_CSVParserTestCase

@synthesize parser;

-(void)setUp
{
    NSString * path = [[NSBundle bundleForClass:[self class] ] pathForResource:@"Test" ofType:@"csv"];

    self.parser = [[BB_CSVParser alloc] initWithContentsOfFile:path];
    [parser release];
}

-(void)tearDown
{
    [parser release];
}

-(void)testGetColumnNames
{
    NSString * col0 = @"ID";
    NSString * col1 = @"String";
    NSString * col2 = @"Number";
    NSString * col3 = @"CommaString";

    // RETRIEVE COLUMN NAMES FROM TEST FILE
    NSArray* columnArray = [parser getColumnNames];
    STAssertNotNil(columnArray, @"Function getColumnNames returned no results");
    
    STAssertEquals( [columnArray count], (NSUInteger)12,
                   @"File Test.csv contains 12 columns");

    
    // VALIDATE EACH NAME    
    STAssertEquals( [columnArray indexOfObject:col0], (NSUInteger)0, 
                   @"Incorrect Column Id");
    STAssertEquals( [columnArray indexOfObject:col1], (NSUInteger)1, 
                   @"Incorrect Column Id");
    STAssertEquals( [columnArray indexOfObject:col2], (NSUInteger)2, 
                   @"Incorrect Column Id");
    STAssertEquals( [columnArray indexOfObject:col3], (NSUInteger)3, 
                   @"Incorrect Column Id");
}

-(void)testGetNextRecord
{
    // CHECK RECORD 1
    NSArray * record = [parser getNextRecord];
    
    STAssertEquals( [[record objectAtIndex:0] intValue] , (NSInteger)1,
                   @"Incorrect value");
    STAssertTrue( NSOrderedSame == [(NSString*)[record objectAtIndex:1] compare: @"Hello"],
                   @"Incorrect value");
    STAssertEquals( [[record objectAtIndex:2] intValue] , (NSInteger)54687,
                   @"Incorrect value");
    STAssertTrue( NSOrderedSame == [(NSString*)[record objectAtIndex:3] compare: @"Three, Two, One. Blast off!"],
                 @"Incorrect value");
    
    // CHECK RECORD 2
    record = [parser getNextRecord];
    STAssertEquals( [[record objectAtIndex:0] intValue] , (NSInteger)2,
                   @"Incorrect value");
    STAssertTrue( NSOrderedSame == [(NSString*)[record objectAtIndex:1] compare: @"Hi"],
                 @"Incorrect value");
    STAssertTrue( NSOrderedSame == [(NSString*)[record objectAtIndex:2] compare: @"123,975"],
                 @"Incorrect value");
    STAssertTrue( NSOrderedSame == [(NSString*)[record objectAtIndex:3] compare: @"He said, \"Wow!\""],
                 @"Incorrect value");
}

-(void)testResetRowCount
{
    
    NSArray * record = [parser getNextRecord];
    
    STAssertEquals( [[record objectAtIndex:0] intValue] , (NSInteger)1,
                   @"Incorrect value");
    STAssertTrue( NSOrderedSame == [(NSString*)[record objectAtIndex:1] compare: @"Hello"],
                 @"Incorrect value");
    STAssertEquals( [[record objectAtIndex:2] intValue] , (NSInteger)54687,
                   @"Incorrect value");
    STAssertTrue( NSOrderedSame == [(NSString*)[record objectAtIndex:3] compare: @"Three, Two, One. Blast off!"],
                 @"Incorrect value");
    
    [parser resetRowCount];
    
    record = [parser getNextRecord];
    
    STAssertEquals( [[record objectAtIndex:0] intValue] , (NSInteger)1,
                   @"Incorrect value");
    STAssertTrue( NSOrderedSame == [(NSString*)[record objectAtIndex:1] compare: @"Hello"],
                 @"Incorrect value");
    STAssertEquals( [[record objectAtIndex:2] intValue] , (NSInteger)54687,
                   @"Incorrect value");
    STAssertTrue( NSOrderedSame == [(NSString*)[record objectAtIndex:3] compare: @"Three, Two, One. Blast off!"],
                 @"Incorrect value");
}


@end
